package cn.zjxiliu.lib_login.ui.login;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import cn.zjxiliu.lib_login.R;
import cn.zjxiliu.lib_login.ui.base.AbsBaseViewImpl;
import cn.zjxiliu.lib_login.ui.forget.ForgetViewImpl;
import cn.zjxiliu.lib_login.ui.register.RegisterViewImpl;
import cn.zjxiliu.lib_login.widget.AccountInputView;

import cn.zjxiliu.lib_login.widget.InputVerificationClickListener;
import cn.zjxiliu.lib_login.widget.InputViewCallbackListener;
import cn.zjxiliu.lib_login.widget.PasswordInputView;
import cn.zjxiliu.lib_login.widget.PhoneInputNumView;
import cn.zjxiliu.lib_login.widget.VerificationCodeInputView;

/**
 * Created by jessie on 2018/11/21.
 */

public class LoginViewImpl extends AbsBaseViewImpl implements ILoginView {

    private static final int REQUEST_CODE_GO_TO_REGISTER = 1;
    private LoginViewHelper loginViewHelper;

    private LinearLayout llBody;

    private ImageView ivExit;

    private TextView tvRegister;

    private TextView tvLoginTitle;

    private ImageView ivLogo;

    private ScrollView scrollMain;


    private LinearLayout llAccountAndPwd;

    private AccountInputView inputAccount;
    private PasswordInputView inputPwd;

    private LinearLayout llPhoneAuth;
    private PhoneInputNumView inputPhone;
    private VerificationCodeInputView inputVerification;

    private TextView tvChangeLoginType;
    private TextView tvForgetPwd;
    private Button btnLogin;


    private boolean accountHasText = false;
    private boolean passwordHasText = false;

    private boolean phonenumHasText = false;
    private boolean verificationcodeHasText = false;

    @Override
    public void initView(int layoutId) {
        setContentView(layoutId);
        llBody = (LinearLayout) this.findViewById(R.id.llBody);
        ivExit = (ImageView) this.findViewById(R.id.ivExit);
        tvRegister = (TextView) this.findViewById(R.id.tvRegister);
        tvLoginTitle = (TextView) this.findViewById(R.id.tvLoginTitle);
        ivLogo = (ImageView) this.findViewById(R.id.ivLogo);
        llAccountAndPwd = (LinearLayout) this.findViewById(R.id.llAccountAndPwd);
        inputAccount = (AccountInputView) this.findViewById(R.id.inputAccount);
        inputPwd = (PasswordInputView) this.findViewById(R.id.inputPwd);
        llPhoneAuth = (LinearLayout) this.findViewById(R.id.llPhoneAuth);
        inputPhone = (PhoneInputNumView) this.findViewById(R.id.inputPhone);
        inputVerification = (VerificationCodeInputView) this.findViewById(R.id.inputVerification);
        tvChangeLoginType = (TextView) this.findViewById(R.id.tvChangeLoginType);
        tvForgetPwd = (TextView) this.findViewById(R.id.tvForgetPwd);
        scrollMain = (ScrollView) this.findViewById(R.id.scrollMain);
        btnLogin = (Button) this.findViewById(R.id.btnLogin);
        btnLogin.setEnabled(false);
        initListener();
    }

    private void initListener() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startLogin();
            }
        });
        inputAccount.setListener(new InputViewCallbackListener() {
            @Override
            public void onFocusChange(boolean isFocus) {

            }

            @Override
            public void inputViewHasText(boolean hasText) {
                accountHasText = hasText;
                changeBtnEnableStatus();
            }
        });
        inputPwd.setListener(new InputViewCallbackListener() {
            @Override
            public void onFocusChange(boolean isFocus) {

            }

            @Override
            public void inputViewHasText(boolean hasText) {
                passwordHasText = hasText;
                changeBtnEnableStatus();
            }
        });

        inputPhone.setListener(new InputViewCallbackListener() {
            @Override
            public void onFocusChange(boolean isFocus) {

            }

            @Override
            public void inputViewHasText(boolean hasText) {
                phonenumHasText = hasText;
                changeBtnEnableStatus();
            }
        });

        inputVerification.setVerificationBtnClickListener(new InputVerificationClickListener() {
            @Override
            public void onVerificationBtnClick() {
                operatorVerificationCodeClick();
            }

            @Override
            public void onFocusChange(boolean isFocus) {

            }

            @Override
            public void inputViewHasText(boolean hasText) {
                verificationcodeHasText = hasText;
                changeBtnEnableStatus();
            }
        });

        listenKeyboardLayout(llBody, btnLogin);
    }

    private void operatorVerificationCodeClick() {
        if(phonenumHasText){
            loginViewHelper.getVerificationCodeWhenPhoneLogin();
        }
    }

    private void changeBtnEnableStatus() {
        if(isAccountAndPwdLoginType()){
            if (accountHasText && passwordHasText) {
                btnLogin.setEnabled(true);
            } else {
                btnLogin.setEnabled(false);
            }
        }else{
            if (phonenumHasText && verificationcodeHasText) {
                btnLogin.setEnabled(true);
            } else {
                btnLogin.setEnabled(false);
            }
        }
    }

    private void startLogin() {
        if(isAccountAndPwdLoginType()){
            loginViewHelper.startLoginByPassword();
        }else{
            loginViewHelper.startLoginByMobileAndVerificationCode();
        }
    }

    @Override
    protected void init() {
        loginViewHelper = new LoginViewHelper(this);
        loginViewHelper.initHelper();
    }

    @Override
    protected void cancelTask() {

    }



    @Override
    public void showLoginTypeView(boolean isPhoneAuthLoginShow, boolean isAccountPwdLoginShow) {
        if(!isPhoneAuthLoginShow || !isAccountPwdLoginShow){
            tvChangeLoginType.setVisibility(View.GONE);
        }else{
            tvChangeLoginType.setVisibility(View.VISIBLE);
            tvChangeLoginType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changePhoneAuthViewOrAccountPwdView(false);
                }
            });
        }

        if(!isAccountPwdLoginShow){
            tvForgetPwd.setVisibility(View.GONE);
        }
    }



    @Override
    public void changeLogoByResId(int logoResId) {
        ivLogo.setVisibility(View.VISIBLE);
        ivLogo.setImageResource(logoResId);
        tvLoginTitle.setVisibility(View.GONE);
    }

    @Override
    public void changeBackgroundImgByResId(int backgroundImgId) {
        scrollMain.setBackgroundResource(backgroundImgId);

    }

    @Override
    public void showBackBtn(boolean isShow) {
        if(isShow){
            ivExit.setVisibility(View.VISIBLE);
            ivExit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }else{
            ivExit.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showRegisterBtn(boolean isShow) {
        if(isShow){
            tvRegister.setVisibility(View.VISIBLE);
            tvRegister.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startToRegisterView();
                }
            });
        }else{
            tvRegister.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 跳转到注册页面流程s
     */
    private void startToRegisterView() {
        startActivityForResult(new Intent(this, RegisterViewImpl.class), REQUEST_CODE_GO_TO_REGISTER);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != this.RESULT_OK){
            return;
        }
        if(requestCode == REQUEST_CODE_GO_TO_REGISTER){
            if(loginViewHelper.getLoginConfig().isFinishedWhenRegisterSuccess()){
                finish();
            }
        }
    }

    @Override
    public void showForgetPwdBtn(boolean isShow) {
        if(isShow){
            tvForgetPwd.setVisibility(View.VISIBLE);
            tvForgetPwd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startToForgetView();
                }
            });
        }else{
            tvForgetPwd.setVisibility(View.GONE);
        }
    }

    /**
     * 跳转到忘记密码页面
     */
    private void startToForgetView() {
        startActivity(new Intent(this, ForgetViewImpl.class));
    }




    @Override
    public void showPhoneAuthLoginViewFirst() {
        changePhoneAuthViewOrAccountPwdView(true);
    }

    private void changePhoneAuthViewOrAccountPwdView(boolean isFirst){
        if(llAccountAndPwd.getVisibility() == View.VISIBLE){
            llAccountAndPwd.setVisibility(View.GONE);
            llPhoneAuth.setVisibility(View.VISIBLE);
            tvChangeLoginType.setText(R.string.login_by_account_and_pwd_title);
            tvLoginTitle.setText(R.string.phone_auth_login);
            tvForgetPwd.setVisibility(View.INVISIBLE);
            if(!isFirst){
                inputPhone.requestFocus();
            }

        }else{
            llAccountAndPwd.setVisibility(View.VISIBLE);
            llPhoneAuth.setVisibility(View.GONE);
            tvChangeLoginType.setText(R.string.phone_auth_login);
            tvLoginTitle.setText(R.string.login_by_account_and_pwd_title);
            if(!isFirst){
                inputAccount.requestFocus();
            }
            if(loginViewHelper.getLoginConfig().isHideForgetPwd()){
                tvForgetPwd.setVisibility(View.GONE);
            }else{
                tvForgetPwd.setVisibility(View.VISIBLE);
            }
        }
        changeBtnEnableStatus();
    }





    @Override
    public boolean isAccountAndPwdLoginType() {
        if(llAccountAndPwd.getVisibility() == View.VISIBLE){
            return true;
        }
        return false;
    }

    @Override
    public String getPassword() {
        return inputPwd.getText().trim();
    }

    @Override
    public String getAccount() {
        return inputAccount.getText();
    }

    @Override
    public String getMobileNo() {
        return inputPhone.getText();
    }

    @Override
    public String getVerificationCode() {
        return inputVerification.getText().trim();
    }

    @Override
    public void showDialogCurrentAccountIsNotRegister() {

    }

    @Override
    public void clearVerificationInput() {
        if(!isAccountAndPwdLoginType()){
            inputVerification.clear();
        }
    }
}
